package com.qq.controller;

import com.qq.pojo.Order;
import com.qq.pojo.OrderCarCityUser;
import com.qq.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@RestController
public class OrderController {

    @Autowired(required = false)
    private OrderService orderService;

    @RequestMapping(value = "/insertOrder",method = RequestMethod.GET)
    public int insertOrder(Order o){
        //System.out.println(o);
        Order order = new Order();
        int i = orderService.insertOrder(o);

        return i;
    }

    /**
     * 个人的订单列表展示
     * @param id
     * @return
     */
    @RequestMapping(value = "/getCarCityOrder", method = RequestMethod.GET)
    public HashMap<String, Object> getCarCityOrder(int id){

        List<OrderCarCityUser> list = orderService.getOrderCarCityUserById(id);


        HashMap<String, Object> map = new HashMap<>();

        map.put("code", 0);
        map.put("count", 1000);
        map.put("msg", "");
        map.put("data", list);

        return map;
    }

    /**
     * 删除订单
     * @param oid   订单的id
     */
    @RequestMapping(value = "/deleteOrder", method = RequestMethod.GET)
    public void deleteOrder(int oid, HttpServletResponse response, HttpServletRequest request){

        int i = orderService.deleteOrder(oid);

        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("code", 0);
        map.put("count", 1000);
        map.put("msg", "");
        map.put("data", true);

        //获取的oid再传入前端页面
        try {
            response.sendRedirect(request.getContextPath() + "/mymain");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
